package ntd.util.frame.action.zzfm.img;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import ntd.action.ActionAbs;
import ntd.action.bo.ActionInfo;
import ntd.action.bo.ActionOutBo;

import java.awt.*;

public class ActionZzfmImgDraw extends ActionAbs {

    @Override
    public ActionInfo info() {
        return new ActionInfo("富铭专用-图片-附加文本", "action-zzfm-img-draw", new ActionZzfmImgDraw());
    }

    private static byte[] img(String text, String imgName) {
        String uuid = IdUtil.simpleUUID();
        String fileName = StrUtil.format("/ntd/temp/{}.png",  uuid);
        String fileNameAfter = StrUtil.format("/ntd/temp/{}-{}.png",  uuid, text);
        HttpUtil.downloadFile(StrUtil.format("http://115.29.192.252:39000/public/map-icon/{}-poly.png", imgName), fileName);
        ImgUtil.pressText(
                FileUtil.file(fileName),
                FileUtil.file(fileNameAfter),
                text, Color.WHITE, //文字
                new Font("黑体", Font.BOLD, 10), //字体
                0, //x坐标修正值。 默认在中间，偏移量相对于中间偏移
                15, //y坐标修正值。 默认在中间，偏移量相对于中间偏移
                1f//透明度：alpha 必须是范围 [0.0, 1.0] 之内（包含边界值）的一个浮点数字
        );
        ImgUtil.scale(FileUtil.file(fileNameAfter), FileUtil.file(fileNameAfter), 1.5f);
        ImgUtil.backgroundRemoval(FileUtil.file(fileNameAfter), FileUtil.file(fileNameAfter), 0);

        byte[] bytes = FileUtil.readBytes(fileNameAfter);

        FileUtil.del(fileName);
//        FileUtil.del(fileNameAfter);

        return bytes;
    }

    public static void main(String[] args) {
        img("23", "cems");
    }

    @Override
    public ActionOutBo handle() {
        String imgName = actionInBo.getParamStr("imgName");
        String text = actionInBo.getParamStr("text");

        return new ActionOutBo(img(text, imgName));
    }
}
